package myfirstproject.firstproject.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import myfirstproject.firstproject.Mapper.ArticleMapper;
import myfirstproject.firstproject.Mapper.UserMapper;
import myfirstproject.firstproject.Service.IArticleService;
import myfirstproject.firstproject.domain.DTO.ArticleDTO;
import myfirstproject.firstproject.domain.DTO.ArticleQueryDTO;
import myfirstproject.firstproject.domain.DTO.ArticleTitleDTO;
import myfirstproject.firstproject.domain.DTO.UserGiveUpDTO;
import myfirstproject.firstproject.domain.VO.ArticleUserVO;
import myfirstproject.firstproject.domain.VO.ArticleVO;
import myfirstproject.firstproject.domain.po.Article;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myfirstproject.firstproject.domain.po.User;
import myfirstproject.firstproject.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-24
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void saveArticle(ArticleDTO articleDTO) {
        Article article = BeanUtil.copyProperties(articleDTO, Article.class);
        User user = userMapper.selectById(articleDTO.getPublisher());
        article.setNickname(user.getNickname());
        articleMapper.insert(article);
    }

    @Override
    public void updateArticleTitle(ArticleTitleDTO articleTitleDTO) {
        Article article = BeanUtil.copyProperties(articleTitleDTO, Article.class);
        articleMapper.updateById(article);
    }

    @Override
    public PageResult<ArticleVO> queryArticlesPage(ArticleQueryDTO query) {
        String title = query.getTitle();
        String publisher = query.getPublisher();
        String id = query.getId();
        String nickname = query.getNickname();
        Page<Article> page = Page.of(query.getPage(), query.getPageSize());
        Page<Article> p = lambdaQuery().like(title != null, Article::getTitle, title)
                .like(publisher != null, Article::getPublisher, publisher)
                .like(id != null, Article::getId, id)
                .like(nickname != null, Article::getNickname, nickname)
                .page(page);
        PageResult<ArticleVO> dto = new PageResult<>();
        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());
        List<Article> records = p.getRecords();
        if(CollUtil.isEmpty(records)){
            dto.setList(Collections.emptyList());
            return dto;
        }
        List<ArticleVO> list = BeanUtil.copyToList(records, ArticleVO.class);
        for (ArticleVO articleVO : list) {
            String publisher1 = articleVO.getPublisher();
            ArticleUserVO articleUserVO = userMapper.queryUserAndAddressById(publisher1);
            articleVO.setArticleUserVO(articleUserVO);
        }
        dto.setList(list);
        return dto;
    }

    @Override
    public void addGiveUp(UserGiveUpDTO userGiveUpDTO) {
        if(articleMapper.ContainerId(userGiveUpDTO.getGiveUpId()) != null){
            throw new RuntimeException("你已经点过赞啦，不要在点了");
        }
        articleMapper.addGiveUpInsert(userGiveUpDTO.getArticleId(),userGiveUpDTO.getGiveUpId());
        articleMapper.addGiveUp(userGiveUpDTO.getArticleId());
    }

    @Override
    public void cancelGiveUp(UserGiveUpDTO userGiveUpDTO) {
        articleMapper.cancelGiveUp(userGiveUpDTO);
        articleMapper.deleteGiveUp(userGiveUpDTO);
    }


}
